package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.EcdPackageDetail;
import com.ruoyi.system.mapper.EcdPackageTransportationMapper;
import com.ruoyi.system.domain.EcdPackageTransportation;
import com.ruoyi.system.service.IEcdPackageTransportationService;

/**
 * 装配包装运输Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-08
 */
@Service
public class EcdPackageTransportationServiceImpl implements IEcdPackageTransportationService 
{
    @Autowired
    private EcdPackageTransportationMapper ecdPackageTransportationMapper;

    /**
     * 查询装配包装运输
     * 
     * @param id 装配包装运输主键
     * @return 装配包装运输
     */
    @Override
    public EcdPackageTransportation selectEcdPackageTransportationById(Long id)
    {
        return ecdPackageTransportationMapper.selectEcdPackageTransportationById(id);
    }

    /**
     * 查询装配包装运输列表
     * 
     * @param ecdPackageTransportation 装配包装运输
     * @return 装配包装运输
     */
    @Override
    public List<EcdPackageTransportation> selectEcdPackageTransportationList(EcdPackageTransportation ecdPackageTransportation)
    {
        return ecdPackageTransportationMapper.selectEcdPackageTransportationList(ecdPackageTransportation);
    }

    /**
     * 新增装配包装运输
     * 
     * @param ecdPackageTransportation 装配包装运输
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEcdPackageTransportation(EcdPackageTransportation ecdPackageTransportation)
    {
        int rows = ecdPackageTransportationMapper.insertEcdPackageTransportation(ecdPackageTransportation);
        insertEcdPackageDetail(ecdPackageTransportation);
        return rows;
    }

    /**
     * 修改装配包装运输
     * 
     * @param ecdPackageTransportation 装配包装运输
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEcdPackageTransportation(EcdPackageTransportation ecdPackageTransportation)
    {
        ecdPackageTransportationMapper.deleteEcdPackageDetailByPtid(ecdPackageTransportation.getId());
        insertEcdPackageDetail(ecdPackageTransportation);
        return ecdPackageTransportationMapper.updateEcdPackageTransportation(ecdPackageTransportation);
    }

    /**
     * 批量删除装配包装运输
     * 
     * @param ids 需要删除的装配包装运输主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEcdPackageTransportationByIds(Long[] ids)
    {
        ecdPackageTransportationMapper.deleteEcdPackageDetailByPtids(ids);
        return ecdPackageTransportationMapper.deleteEcdPackageTransportationByIds(ids);
    }

    /**
     * 删除装配包装运输信息
     * 
     * @param id 装配包装运输主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEcdPackageTransportationById(Long id)
    {
        ecdPackageTransportationMapper.deleteEcdPackageDetailByPtid(id);
        return ecdPackageTransportationMapper.deleteEcdPackageTransportationById(id);
    }

    /**
     * 新增包装详情信息
     * 
     * @param ecdPackageTransportation 装配包装运输对象
     */
    public void insertEcdPackageDetail(EcdPackageTransportation ecdPackageTransportation)
    {
        List<EcdPackageDetail> ecdPackageDetailList = ecdPackageTransportation.getEcdPackageDetailList();
        Long id = ecdPackageTransportation.getId();
        if (StringUtils.isNotNull(ecdPackageDetailList))
        {
            List<EcdPackageDetail> list = new ArrayList<EcdPackageDetail>();
            for (EcdPackageDetail ecdPackageDetail : ecdPackageDetailList)
            {
                ecdPackageDetail.setPtid(id);
                list.add(ecdPackageDetail);
            }
            if (list.size() > 0)
            {
                ecdPackageTransportationMapper.batchEcdPackageDetail(list);
            }
        }
    }
}
